﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BbsbWebService.Tasks;
using BbsbWebService.Repositories;
using BbsbWebService;
using System.Collections.ObjectModel;

namespace BbsbWsGui
{
    /// <summary>
    /// Interaction logic for TaskTab.xaml
    /// </summary>
    public partial class TaskTab : UserControl
    {
        public TaskTab()
        {
            InitializeComponent();
        }

        #region Task Methods

        private void addTaskBtn_Click(object sender, RoutedEventArgs e)
        {
            TaskDetails dialog = new TaskDetails();
            if (dialog.ShowDialog() == true &&
                dialog.Task != null)
            {
                RepositoryInstances.TaskRepository.SaveItem(dialog.Task);
                //RefreshItems();
            }
        }

        private void editTaskBtn_Click(object sender, RoutedEventArgs e)
        {
            var item = tasksLst.SelectedItem as BaseTask;
            if (item != null)
            {
                TaskDetails dialog = new TaskDetails(item);
                if (dialog.ShowDialog() == true &&
                    dialog.Task != null)
                {
                    RepositoryInstances.TaskRepository.SaveItem(dialog.Task);
                }
            }
        }

        private void removeTaskBtn_Click(object sender, RoutedEventArgs e)
        {
            var item = tasksLst.SelectedItem as BaseTask;
            if (item != null)
            {
                RepositoryInstances.TaskRepository.RemoveItem(item);
                //RefreshItems();
            }
        }

        private void tasksLst_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string description = string.Empty;

            var item = tasksLst.SelectedItem as BaseTask;
            if (item != null)
            {
                description = item.Description;
            }

            taskDescription.Content = description;
        }

        #endregion

        public bool Enabled
        {
            set
            {
                bool enabled = value;
                tasksLst.IsEnabled = enabled;
                addTaskBtn.IsEnabled = enabled;
                editTaskBtn.IsEnabled = enabled;
                removeTaskBtn.IsEnabled = enabled;
            }
        }

        public ObservableCollection<BaseTask> ItemsSource
        {
            set
            {
                tasksLst.ItemsSource = value;
            }
        }

    }
}
